package com.example.assignment01_javaee.Controller;

import com.example.assignment01_javaee.Entity.Order;
import com.example.assignment01_javaee.Service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/orders")
    public ResponseEntity<List<Order>> getAllOrders() {
        List<Order> orders = orderService.getAllOrders();
        return ResponseEntity.ok(orders);
    }

    @GetMapping("/orders/{orderId}")
    public ResponseEntity<Order> getOrderById(@PathVariable int orderId) {
        Order order = orderService.getOrderById(orderId);
        return ResponseEntity.ok(order);
    }

    @PostMapping("/orders")
    public String createOrder(@RequestBody Order order) {
        System.out.println(order.toString());
        return orderService.createOrder(order);
    }

    //修改下单数量
    @PutMapping("/orders/{orderId}")
    public String updateOrder(@PathVariable int orderId, @RequestParam int count) {
        return orderService.updateOrder(orderId,count);
    }

    @DeleteMapping("/orders/{orderId}")
    public ResponseEntity<Void> deleteOrder(@PathVariable int orderId) {
        orderService.deleteOrder(orderId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
